<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>ToDoList</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="inputValue">
        <button v-on:click="clickOnBtn">提交</button>
        <ul>
            <todo-item v-for="(item, index) in list" v-bind:content="item" v-bind:index="index" @delete="ItemDelete">
            </todo-item>
        </ul>
    </div>

    <script>
        var TodoItem = {
            props: ['content', 'index'],
            methods: {
                clickOnItem: function () {
                    this.$emit("delete", this.index)
                }
            },
            template: "<li @click='clickOnItem'>{{content}}</li>"
        }

        var app = new Vue({
            el: '#app',
            components: {
                TodoItem: TodoItem
            },
            data: {
                inputValue: '',
                list: []
            },
            methods: {
                clickOnBtn: function () {
                    if (this.inputValue != '') {
                        this.list.push(this.inputValue),
                            this.inputValue = ''
                    } else {
                        alert('输入为空！')
                    }
                },
                ItemDelete: function (index) {
                    this.list.splice(index, 1)
                }
            }
        })

    </script>
</body>

</html>